using FireStarterModeller.BO;
using FireStarterModeller.UI;
using Habanero.Base;
using Habanero.BO;
using Habanero.Faces.Base;
using Habanero.Faces.Win;
using NUnit.Framework;

namespace FireStarterModeller.Test.UI
{
    [TestFixture]
    public  class TestDMClassGridSelectorControl
    {
        [TestFixtureSetUp]
        public void SetupFixture()
        {
            UITestUtils.SetupFixture();
            GlobalRegistry.UIExceptionNotifier = new RethrowingExceptionNotifier();
        }


        [SetUp]
        public virtual void SetupTest()
        {
            UITestUtils.SetupTest();

        }
        [Test]
        public void Test_Construction()
        {
            //---------------Set up test pack-------------------
            
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            DMClassGridSelectorControl control =
                new DMClassGridSelectorControl(new ControlFactoryWin());
            //---------------Test Result -----------------------
            Assert.AreEqual(1, control.Controls.Count);
            Assert.IsInstanceOf(typeof(IReadOnlyGridControl), control.Controls[0]);
            IGridControl readGridControl = (IGridControl) control.Controls[0];
            Assert.IsFalse(readGridControl.Buttons.Visible);
            Assert.IsTrue(readGridControl.FilterControl.Visible);
            Assert.AreEqual(FilterModes.Filter, readGridControl.FilterMode);
            Assert.AreEqual(1, readGridControl.FilterControl.FilterControls.Count);
            Assert.AreEqual("ClassNameBO", readGridControl.FilterControl.FilterControls[0].PropertyName);

        }

        //[Test]
        //public void Test_GridControlDoesNotShowButtons()
        //{
        //    //---------------Set up test pack-------------------

        //    //---------------Assert Precondition----------------

        //    //---------------Execute Test ----------------------
        //    DMClassGridSelectorControl control =
        //        new DMClassGridSelectorControl(new ControlFactoryWin());
        //    //---------------Test Result -----------------------
        //    Assert.AreEqual(1, control.Controls.Count);
        //    Assert.IsInstanceOf(typeof(IGridControl), control.Controls[0]);
        //    IGridControl gridControl = (IGridControl)control.Controls[0];
        //    IButtonGroupControl buttonsControl = gridControl.Buttons;
        //    Assert.IsFalse(buttonsControl.Visible);
        //}


        [Test]
        public void TestDisabledAtCreation()
        {
            //---------------Execute Test ----------------------
            DMClassGridSelectorControl control =
                new DMClassGridSelectorControl(new ControlFactoryWin());
            //---------------Test Result -----------------------

            Assert.IsFalse(control.Enabled);
        }

        [Test]
        public void TestSetEnables()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateUnsavedValidDMSolution();
            DMClassGridSelectorControl control = new DMClassGridSelectorControl(new ControlFactoryWin());

            //---------------Execute Test ----------------------
            control.Solution = solution;
            //---------------Test Result -----------------------
            Assert.IsTrue(control.Enabled);
        }

        [Test]
        public void TestSetToNullDisables()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateUnsavedValidDMSolution();
            DMClassGridSelectorControl control = new DMClassGridSelectorControl(new ControlFactoryWin());
            control.Solution = solution;
            //---------------Execute Test ----------------------
            control.Solution = null;
            //---------------Test Result -----------------------
            Assert.IsFalse(control.Enabled);
            Assert.AreEqual(0, control.Classes.Count);

        }

        [Test]
        public void TestSetBusinessObject()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateUnsavedValidDMSolution();
            DMAssembly assembly = solution.CreateAssembly(TestUtilsShared.GetRandomString());
            DMClass dmClass = assembly.CreateClass("Class1");
            DMClassGridSelectorControl control = new DMClassGridSelectorControl(new ControlFactoryWin());
            control.Solution = solution;
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            control.Class = dmClass;
            //---------------Test Result -----------------------
            Assert.AreSame(dmClass, control.Class);
            Assert.AreSame(control.Class, control.GridControl.SelectedBusinessObject);
        }


        //[Ignore("Changing how this works")] 
        [Test]
        public void TestBusinessObjectSelectedEvent()
        {
            //---------------Set up test pack-------------------
            DMSolution solution = TestUtilsShared.CreateUnsavedValidDMSolution();
            DMAssembly assembly = solution.CreateAssembly(TestUtilsShared.GetRandomString());
            DMClass dmClass = assembly.CreateClass("Class1");
            DMClassGridSelectorControl control =
                new DMClassGridSelectorControl(new ControlFactoryWin());
            control.Solution = solution;
            bool fired = false;
            DMClass selected = null;
            //---------------Assert PreConditions---------------            
            //---------------Execute Test ----------------------
            control.ClassSelected +=
                (sender, e) =>
                {
                    fired = true;
                    selected = e.BusinessObject;
                };
            control.Class = dmClass;
            //---------------Test Result -----------------------

            Assert.IsTrue(fired);
            Assert.AreSame(selected, control.Class);
            //---------------Tear Down -------------------------          
        }


        //[Ignore("Changing how this works")] 
        //[Test]
        //public void TestNewButtonClickedCreatesBO_EmptyCollection()
        //{
        //    //---------------Set up test pack-------------------
        //    DMSolution solution = TestUtilsShared.CreateUnsavedValidDMSolution();
        //    DMClassGridSelectorControl classSelectorControl =
        //        new DMClassGridSelectorControl(new ControlFactoryWin()); 
        //    IGridControl readGridControl = classSelectorControl.GridControl;
        //    classSelectorControl.Assembly = assembly;
        //    //  ---------------Assert Precondition----------------
        //    Assert.AreEqual(0, readGridControl.Grid.Rows.Count);
        //    BusinessObjectCollection<DMClass> classes = assembly.Classes;
        //    Assert.AreEqual(0, classes.Count);
        //    //  ---------------Execute Test ----------------------
        //    readGridControl.Buttons["New"].PerformClick();
        //    // ---------------Test Result -----------------------
        //    Assert.AreEqual(1, readGridControl.Grid.Rows.Count);
        //    Assert.AreEqual(1, classes.Count);
        //    Assert.AreSame(classes[0], readGridControl.SelectedBusinessObject);
        //}
        //[Ignore("Changing how this works")] 
        //[Test]
        //public void TestNewButtonClickedCreatesBO_ExistingCollection()
        //{
        //    //---------------Set up test pack-------------------
        //    DMSolution solution = TestUtilsShared.CreateUnsavedValidDMSolution();
        //    TestUtilsShared.CreateSavedDMClass(assembly);
        //    DMClassGridSelectorControl classSelectorControl =
        //        new DMClassGridSelectorControl(new ControlFactoryWin());
        //    IGridControl readGridControl = classSelectorControl.GridControl;
        //    classSelectorControl.Assembly = assembly;
        //    //  ---------------Assert Precondition----------------
        //    Assert.AreEqual(1, readGridControl.Grid.Rows.Count);
        //    BusinessObjectCollection<DMClass> classes = assembly.Classes;
        //    Assert.AreEqual(1, classes.Count);
        //    //  ---------------Execute Test ----------------------
        //    readGridControl.Buttons["New"].PerformClick();
        //    // ---------------Test Result -----------------------
        //    Assert.AreEqual(2, readGridControl.Grid.Rows.Count);
        //    Assert.AreEqual(2, classes.Count);
        //    Assert.AreSame(classes[1], readGridControl.SelectedBusinessObject);
        //}
        //[Ignore("Changing how this works")] 
        //[Test]
        //public void TestDeleteButtonDeletesCurrentBO()
        //{
        //    // ---------------Set up test pack-------------------
        //    //---------------Set up test pack-------------------
        //    DMSolution solution = TestUtilsShared.CreateUnsavedValidDMSolution();
        //    TestUtilsShared.CreateSavedDMClass(assembly);
        //    BusinessObjectCollection<DMClass> classes = assembly.Classes;
        //    DMClassGridSelectorControl classSelectorControl =
        //        new DMClassGridSelectorControl(new ControlFactoryWin());
        //    IGridControl readGridControl = classSelectorControl.GridControl;
        //    classSelectorControl.Assembly = assembly;
        //    IBusinessObject currentBO = readGridControl.SelectedBusinessObject;
        //    //  ---------------Assert Precondition----------------
        //    Assert.AreEqual(1, readGridControl.Grid.Rows.Count);
        //    Assert.AreEqual(1, classes.Count);
        //    //  ---------------Execute Test ----------------------
        //    readGridControl.Buttons["Remove"].PerformClick();
        //    // ---------------Test Result -----------------------
        //    Assert.IsTrue(currentBO.Status.IsDeleted);
        //    Assert.IsFalse(currentBO.Status.IsDirty);
        //    Assert.AreEqual(0, readGridControl.Grid.Rows.Count);
        //    Assert.AreEqual(0, classes.Count);
        //}
        //[Ignore("Changing how this works")] 
        //[Test]
        //public void TestDeleteButton_NoCurrentBO()
        //{
        //    //---------------Set up test pack-------------------
        //    DMSolution solution = TestUtilsShared.CreateUnsavedValidDMSolution();
        //    BusinessObjectCollection<DMClass> classes = assembly.Classes;
        //    DMClassGridSelectorControl classSelectorControl =
        //        new DMClassGridSelectorControl(new ControlFactoryWin());
        //    IGridControl readGridControl = classSelectorControl.GridControl;
        //    classSelectorControl.Assembly = assembly;
        //    IBusinessObject currentBO = readGridControl.SelectedBusinessObject;
        //    //  ---------------Assert Precondition----------------
        //    Assert.AreEqual(0, readGridControl.Grid.Rows.Count);
        //    Assert.AreEqual(0, classes.Count);
        //    Assert.IsNull(currentBO);
        //    //  ---------------Execute Test ----------------------
        //    readGridControl.Buttons["Remove"].PerformClick();
        //    // ---------------Test Result -----------------------
        //    Assert.AreEqual(0, readGridControl.Grid.Rows.Count);
        //    Assert.AreEqual(0, classes.Count);
        //}
        //[Ignore("Changing how this works")] 
        //[Test]
        //public void TestCancelButton_ClickRestoresSavedObject()
        //{
        //    //---------------Set up test pack-------------------
        //    DMSolution solution = TestUtilsShared.CreateUnsavedValidDMSolution();
        //    TestUtilsShared.CreateSavedDMClass(assembly);
        //    BusinessObjectCollection<DMClass> classes = assembly.Classes;
        //    DMClassGridSelectorControl classSelectorControl =
        //        new DMClassGridSelectorControl(new ControlFactoryWin());
        //    IGridControl readGridControl = classSelectorControl.GridControl;
        //    classSelectorControl.Assembly = assembly;
        //    DMClass currentBO = (DMClass) readGridControl.SelectedBusinessObject;
        //    currentBO.DisplayName = TestUtilsShared.GetRandomString();
        //    //  ---------------Assert Precondition----------------
        //    Assert.AreEqual(1, readGridControl.Grid.Rows.Count);
        //    Assert.AreEqual(1, classes.Count);
        //    Assert.IsNotNull(currentBO);
        //    Assert.IsTrue(currentBO.Status.IsDirty);
        //    //  ---------------Execute Test ----------------------
        //    readGridControl.Buttons["Cancel"].PerformClick();
        //    // ---------------Test Result -----------------------
        //    Assert.IsFalse(currentBO.Status.IsDirty);
        //}
        //[Ignore("Changing how this works")] 
        //[Test]
        //public void TestCancelButton_Click_NoObjecctSelected()
        //{
        //    //---------------Set up test pack-------------------
        //    DMSolution solution = TestUtilsShared.CreateUnsavedValidDMSolution();
        //    BusinessObjectCollection<DMClass> classes = assembly.Classes;
        //    DMClassGridSelectorControl classSelectorControl =
        //        new DMClassGridSelectorControl(new ControlFactoryWin());
        //    IGridControl readGridControl = classSelectorControl.GridControl;
        //    classSelectorControl.Assembly = assembly;
        //    DMClass currentBO = (DMClass) readGridControl.SelectedBusinessObject;
        //    //  ---------------Assert Precondition----------------
        //    Assert.AreEqual(0, readGridControl.Grid.Rows.Count);
        //    Assert.AreEqual(0, classes.Count);
        //    Assert.IsNull(currentBO);
        //    //  ---------------Execute Test ----------------------
        //    readGridControl.Buttons["Cancel"].PerformClick();
        //    // ---------------Test Result -----------------------
        //    Assert.AreEqual(0, readGridControl.Grid.Rows.Count);
        //    Assert.AreEqual(0, classes.Count);
        //}
        //[Ignore("Changing how this works")] 
        //[Test]
        //public void TestCancelButton_ClickRemovesNewObject_OnlyItemInGrid()
        //{
        //    //---------------Set up test pack-------------------
        //    DMSolution solution = TestUtilsShared.CreateUnsavedValidDMSolution();
        //    TestUtilsShared.CreateSavedDMClass(assembly);
        //    BusinessObjectCollection<DMClass> classes = assembly.Classes;
        //    DMClassGridSelectorControl classSelectorControl =
        //        new DMClassGridSelectorControl(new ControlFactoryWin());
        //    IGridControl readGridControl = classSelectorControl.GridControl;
        //    classSelectorControl.Assembly = assembly;
        //    IButton cancelButton = readGridControl.Buttons["Cancel"];
        //    IButton newButton = readGridControl.Buttons["New"];

        //    newButton.PerformClick();
        //    //---------------Assert Precondition----------------
        //    Assert.IsTrue(cancelButton.Enabled);
        //    Assert.AreEqual(2, readGridControl.Grid.Rows.Count);
        //    //---------------Execute Test ----------------------
        //    cancelButton.PerformClick();
        //    //---------------Test Result -----------------------
        //    Assert.AreEqual(1, classes.Count, "The new item should be removed from the collection");
        //    Assert.AreEqual(1, readGridControl.Grid.Rows.Count);
        //}

        //[Ignore("Changing how this works")] 
        //[Test]
        //public void TestCancelButton_ClickRemovesNewObject_TwoItemsInGrid()
        //{
        //    // ---------------Set up test pack-------------------
        //    DMSolution solution = TestUtilsShared.CreateUnsavedValidDMSolution();
        //    TestUtilsShared.CreateSavedDMClass(assembly);
        //    TestUtilsShared.CreateSavedDMClass(assembly);
        //    DMClassGridSelectorControl classSelectorControl =
        //        new DMClassGridSelectorControl(new ControlFactoryWin());
        //    IGridControl readGridControl = classSelectorControl.GridControl;
        //    classSelectorControl.Assembly = assembly;
        //    IButton cancelButton = readGridControl.Buttons["Cancel"];
        //    IButton newButton = readGridControl.Buttons["New"];
        //    newButton.PerformClick();
        //    // ---------------Assert Precondition----------------
        //    Assert.AreEqual(3, readGridControl.Grid.Rows.Count);
        //    //   ---------------Execute Test ----------------------
        //    cancelButton.PerformClick();
        //    //  ---------------Test Result -----------------------
        //    Assert.AreEqual(2, readGridControl.Grid.Rows.Count);
        //    Assert.IsNotNull(readGridControl.SelectedBusinessObject);
        //}
        //[Ignore("Changing how this works")] 
        //[Test]
        //public void Test_ObjectSavesWhenSaveButtonClicked()
        //{
        //    //---------------Set up test pack-------------------
        //    DMSolution solution = TestUtilsShared.CreateUnsavedValidDMSolution();
        //    TestUtilsShared.CreateSavedDMClass(assembly);
        //    DMClassGridSelectorControl classSelectorControl =
        //        new DMClassGridSelectorControl(new ControlFactoryWin());
        //    IGridControl readGridControl = classSelectorControl.GridControl;
        //    classSelectorControl.Assembly = assembly;
        //    readGridControl.Buttons["New"].PerformClick();
        //    DMClass currentBO = (DMClass)readGridControl.SelectedBusinessObject;
        //    TestUtilsShared.UpdateBusinessObjectWithValidData(currentBO);
        //    currentBO.DisplayName = TestUtilsShared.GetRandomString();

        //    //---------------Assert Precondition----------------
        //    Assert.IsNotNull(currentBO);
        //    Assert.IsTrue(currentBO.Status.IsNew);
        //    Assert.IsTrue(currentBO.Status.IsDirty);
        //    Assert.IsTrue(currentBO.Status.IsValid());
        //    //  ---------------Execute Test ----------------------
        //    readGridControl.Buttons["Save"].PerformClick();
        //    // ---------------Test Result -----------------------
        //    Assert.IsFalse(currentBO.Status.IsDirty);
        //    Assert.IsFalse(currentBO.Status.IsNew);
        //    Assert.IsFalse(currentBO.Status.IsDeleted);
        //}
        //[Ignore("Changing how this works")] 
        //[Test]
        //public void Test_SaveButtonClicked_NoObjectSelected()
        //{
        //    //---------------Set up test pack-------------------
        //    DMSolution solution = TestUtilsShared.CreateUnsavedValidDMSolution();
        //    DMClassGridSelectorControl classSelectorControl =
        //        new DMClassGridSelectorControl(new ControlFactoryWin());
        //    IGridControl readGridControl = classSelectorControl.GridControl;
        //    classSelectorControl.Assembly = assembly;
        //    DMClass currentBO = (DMClass)readGridControl.SelectedBusinessObject;
        //    //---------------Assert Precondition----------------
        //    Assert.IsNull(currentBO);
        //    Assert.AreEqual(0, readGridControl.Grid.Rows.Count);
        //    //  ---------------Execute Test ----------------------
        //    readGridControl.Buttons["Save"].PerformClick();
        //    // ---------------Test Result -----------------------
        //    Assert.AreEqual(0, readGridControl.Grid.Rows.Count);
        //}
    }
}